package com.atguigu.gmall.web.all.controller;

import com.atguigu.gmall.feign.cart.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    CartFeignClient cartFeignClient;

    /**
     * skuId=4&skuNum=1&sourceType=QUREY
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("addCart.html")
    public String addCartHtml(@RequestParam("skuId") Long skuId,
                              @RequestParam("skuNum") Integer skuNum,
                              Model model){
        //远程调用购物车功能，将此商品添加到购物车

        //返回skuInfo信息即可：skuDefaultImag,skuName,id
        CartInfo cartInfo = cartFeignClient.addToCart(skuId,skuNum);

        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(cartInfo.getSkuId());
        skuInfo.setSkuName(cartInfo.getSkuName());
        skuInfo.setSkuDefaultImg(cartInfo.getImgUrl());

        model.addAttribute("skuInfo", skuInfo);
        model.addAttribute("skuNum",skuNum);

        return "cart/addCart";
    }

    /**
     * 购物车列表页
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListHtml(){
        //页面自己发ajax,不用远程调用
        //List<CartInfo> cartInfos = cartFeignClient.listCart();

        return "cart/index";
    }
}
